package com.reebake.ideal.system.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.reebake.ideal.system.entity.QuickNavigation;
import com.reebake.ideal.holder.UserContextHolder;
import com.reebake.ideal.system.entity.WorkPending;
import com.reebake.ideal.system.service.QuickNavigationService;
import com.reebake.ideal.system.service.WorkPendingService;
import com.reebake.ideal.system.vo.QuickNavigationVO;
import com.reebake.ideal.system.vo.WorkPendingVO;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RequiredArgsConstructor
@RestController
@RequestMapping("/workbench")
public class WorkbenchController {
    @Autowired
    private WorkPendingService workPendingService;
    @Autowired
    private QuickNavigationService quickNavigationService;

    @PostMapping("work-pending")
    public List<WorkPendingVO> workPending() {
        String userId = UserContextHolder.getCurrentUserId();
        LambdaQueryWrapper<WorkPending> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WorkPending::getUserId, userId).or().isNull(WorkPending::getUserId);

        List<WorkPending> list = workPendingService.list(queryWrapper);

        return BeanUtil.copyToList(list, WorkPendingVO.class);
    }

    @PostMapping("quick-navigation")
    public List<QuickNavigationVO> quickNavigation() {
        String userId = UserContextHolder.getCurrentUserId();
        LambdaQueryWrapper<QuickNavigation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(QuickNavigation::getUserId, userId).or().isNull(QuickNavigation::getUserId)
                .orderByAsc(QuickNavigation::getOrderNo);

        List<QuickNavigation> list = quickNavigationService.list(queryWrapper);

        return BeanUtil.copyToList(list, QuickNavigationVO.class);
    }

}
